/*
 * SuperBaseFragment.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yidiandao.business.R;
import com.yidiandao.business.interfaces.RefreshLayoutStateListener;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.List;
import java.util.Map;

/**
 * 具备控制页面网络状态功能的BaseFragment
 */
public abstract class SuperBaseFragment extends RootFragment {

    /**
     * Fragment根布局
     */
    protected View mRootView;

    private SuperLoadingLayout mLoadingLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (mRootView == null) {
            mRootView = inflater.inflate(getLayoutId(), container, false);

            mLoadingLayout = (SuperLoadingLayout) mRootView.findViewById(R.id.loading_pager);

            if (mLoadingLayout == null) {
                throw new IllegalArgumentException("You must add " + SuperLoadingLayout.class.getName() + " to " + this.getClass().getName() + "'s layout first!!!");
            }

            // 初始化View
            initViews();

            // 考虑到某些页面不一定只加载一次数据,加入判断
            // 加载数据
            if (!isMultiLoadData()) {
                triggerLoadData();
            }

        } else {
            ViewGroup viewGroup = (ViewGroup) mRootView.getParent();
            if (viewGroup != null) {
                viewGroup.removeView(mRootView);
            }
        }

        return mRootView;
    }

    private void triggerLoadData() {
        if (null != mLoadingLayout) {
            mLoadingLayout.triggerLoadData(new SuperLoadingLayout.OnStartLoadDataListener() {
                @Override
                public void onStartLoadData() {
                    SuperBaseFragment.this.startLoadData();
                }
            });
        }
    }

    /**
     * 是否需要多次刷新数据
     */
    protected boolean isMultiLoadData() {
        return false;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (isMultiLoadData()) {
            triggerLoadData();
        }
    }

    /**
     * 触发页面加载数据.可以通过调用{@link BasePresenter#start()}方法来加载数据
     */
    protected abstract void startLoadData();

    /**
     * 根据状态刷新界面的UI.<b>该方法需要在{@link RefreshLayoutStateListener#refreshContent(int)}方法中调用,以修改Layout状态：</b>
     *
     * @param result one of
     *               {@link SuperLoadingLayout#STATE_SUCCESS},
     *               {@link SuperLoadingLayout#STATE_LOADING},
     *               {@link SuperLoadingLayout#STATE_NONE},
     *               {@link SuperLoadingLayout#STATE_EMPTY},
     *               {@link SuperLoadingLayout#STATE_ERROR}
     */
    protected void superRefreshLayoutState(@SuperLoadingLayout.LoadState int result) {
        if (null != mLoadingLayout) mLoadingLayout.refreshState(result);
    }

    /**
     * 检查加载结果是否为空
     *
     * @return one of
     * {@link SuperLoadingLayout#STATE_SUCCESS},
     * {@link SuperLoadingLayout#STATE_EMPTY}
     */
    @SuperLoadingLayout.LoadState
    protected int superCheckListState(Object resResult) {
        if (resResult == null) {
            return SuperLoadingLayout.STATE_EMPTY;
        }
        if (resResult instanceof List) {
            if (((List) resResult).size() == 0) {
                return SuperLoadingLayout.STATE_EMPTY;
            }
        }
        if (resResult instanceof Map) {
            if (((Map) resResult).size() == 0) {
                return SuperLoadingLayout.STATE_EMPTY;
            }
        }
        return SuperLoadingLayout.STATE_SUCCESS;
    }
}
